close all; clear all; path(pathdef); clc;

% add subfunctions
addpath(genpath('subfunctions/'))
addpath(genpath('COMSOL_simulation/'))
addpath(genpath('data_figure2/'))

% color library
color_list{1} = [67 84 147]/255;        % dark blue
color_list{2} = [110 153 201]/255;      % light blue
color_list{3} = [196 40 27]/255;        % dark red
color_list{4} = [218 134 121]/255;      % light red
color_list{5} = [40 127 70]/255;        % dark green
color_list{6} = [161 196 139]/255;      % light green
color_list{7} = [218 165 32]/255;       % gold
color_list{8} = [238 221 130]/255;      % light gold
color_list{9} = [128 0 128]/255;        % purple
color_list{10} = [204 153 255]/255;     % light purple
color_list{11} = [161 165 162]/255;     % grey


%% Load data
load('ESR-2018-10-17_19-14-15/gResultData.mat');

fh = figure(1); cla()
set(fh, 'position', [100 100 [170 140]])

xdata = gResultData.Freq/1e9;
ydata = mean(gResultData.ESR,1);
ydataerr = std(gResultData.ESR,1)/sqrt(size(gResultData.ESR,1));

fitplot({xdata, ydata, ydataerr}, 'NormInvLorentzianSplitting', color_list{2});
hold on

bPlotOption.MarkerSize  = 4;
bPlotOption.CapSize     = 0;
bPlotOption.LineWidth   = 1;
bPlotOption.FontSize    = 12;
bPlotOption.MarkerList = {'o', 's', 'o', 'o', 'o', 'o'};

color_idx = 1;
marker_idx = 1;
plotcustom(xdata, ydata, ydataerr, bPlotOption, marker_idx, color_idx)

dw = 0.001;
f1 = [-dw dw]+2.8695-0.006;
f2 = [-dw dw]+2.8695+0.006;
y1 = [0.91 0.88];
y2 = fliplr(y1);

xvals = linspace(2.85, 2.89, 100);
plot(xvals, (y1(2)-y1(1))/(f1(2)-f1(1))*xvals + (y1(2)-(y1(2)-y1(1))/(f1(2)-f1(1))*f1(2)), ':', 'color', color_list{4}, 'linewidth', 2)
plot(xvals, (y2(2)-y2(1))/(f2(2)-f2(1))*xvals + (y2(2)-(y2(2)-y2(1))/(f2(2)-f2(1))*f2(2)), ':', 'color', color_list{4}, 'linewidth', 2)

plot(f1, y1, 'x', 'markersize', 8, 'color', color_list{3}, 'linewidth', 2)
plot(f2, y2, 'x', 'markersize', 8, 'color', color_list{3}, 'linewidth', 2)

axisinfo('Frequency (GHz)', 'Contrast', [2.85 2.89], [0.805 1], bPlotOption.FontSize)
set(gca, 'XTick', [2.85 2.87 2.89])
set(gca, 'YTick', [0.8 0.9 1])
